<script lang="ts" setup>
import { getCardPay } from '@/api/car';
import type { ParkingParams, PayRow } from '@/types/car';
import { onMounted, ref } from 'vue';
import { utils, writeFileXLSX } from 'xlsx';

const Params=ref<ParkingParams>({
  page:1,
  pageSize:10
})
const ruleList = ref<PayRow[]>([])
const total=ref(0)
const getPayList=async()=>{
  const res=await getCardPay(Params.value)
  ruleList.value=res.data.rows
  total.value=res.data.total
}
onMounted(()=>{
  getPayList()
})
// 导出Excel
const exportToExcel = () => {
  // 1. 创建工作簿 (xlsx文件)
  const workbook = utils.book_new()
  // 2. 创建工作表 (表格)
  const worksheet = utils.json_to_sheet(ruleList.value.map((item, index) => ({
    ...item,
    id: index + 1, // 把 id 覆盖成序号
    // chargeType: chargeTypeMap.get(item.chargeType) // 把 chargeType 覆盖成中文
  })))
  // 3. 往工作簿中添加表格 (工作簿, 表格, 表格名)
  utils.book_append_sheet(workbook, worksheet, '计费规则')
  // 4. 覆盖默认标题 (表格, 二维数组, 标题位置)
  utils.sheet_add_aoa(
    worksheet,
    // aoa -> array of arrays -> 二维数组
    [
      ['序号', '计费规则编号', '计费规则名称', '免费时长(分钟)', '收费上限(元)', '计费方式', '计费规则'],
    ],
    { origin: 'A1' }
  )
  // 4. 导出(下载) xlsx 文件
  writeFileXLSX(workbook, '停车计费规则.xlsx')
}
</script>

<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary">增加停车计费规则</el-button>
      <el-button @click="exportToExcel">导出Excel</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上限(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式">
          <template #default="{ row }">
            <el-tag v-if="row.chargeType === 'duration'">时长收费</el-tag>
            <el-tag v-else-if ="row.chargeType === 'turn'">按次收费</el-tag>
            <el-tag v-else-if="row.chargeType === 'partition'">分段收费</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="计费规则" prop="ruleNameView" width="320" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default>
            <el-button size="small" type="primary" link>编辑</el-button>
            <el-button size="small" type="primary" link>删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
      v-model:current-page="Params.page"
      v-model:page-size="Params.pageSize"
      :page-sizes="[1, 2, 3, 4]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @change="getPayList"
    />
    </div>
  </div>
</template>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.rules .el-form-item {
  padding: 0 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>